package cn.rose.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author Administrator
 * 获取关键字出现次数算法类
 */
public class CopyOfGetCount {
	
	public static void main(String[] args) {
		
		Date dateStart = new Date();
		
		
		String file1 = "E:\\javaworkspace\\google_svn_project\\src\\cn\\rose\\test\\uckey.txt";
		String file2 = "E:\\javaworkspace\\google_svn_project\\src\\cn\\rose\\test\\sum_citytemplet.txt";
		String file3 = "E:\\javaworkspace\\google_svn_project\\src\\cn\\rose\\test\\sum_peopletemplet.txt";
		String file4 = "E:\\javaworkspace\\google_svn_project\\src\\cn\\rose\\test\\count.txt";
		CopyOfGetCount getCount = new CopyOfGetCount();
		List uckeyList = getCount.getListFromFile(file1,"UTF-8");
		List cityList = getCount.getListFromFile(file2,"GBK");
		List peopleList = getCount.getListFromFile(file3,"GBK");
		List countList = getCount.getListFromFile(file4,"UTF-8");
		HashMap<String,Integer> cityHashMap = new HashMap<String,Integer>();
		HashMap<String,Integer> peopleHashMap = new HashMap<String,Integer>();
		//对比城市
		for(int i = 0; i<cityList.size(); i++){
			String cityStr = cityList.get(i) == null ? "" : cityList.get(i).toString();
			for (int j = 0; j < uckeyList.size(); j++) {
				String uckeyStr = uckeyList.get(j) == null ? "" : uckeyList.get(j).toString();
				if (cityStr.length() == uckeyStr.length() && cityStr.equals(uckeyStr)) {
					cityHashMap.put(cityStr, j);
					break;
				}
			}
		}
		//对比人物
		for(int i = 0; i<peopleList.size(); i++){
			String peopleStr = peopleList.get(i) == null ? "" : peopleList.get(i).toString();
			for (int j = 0; j < uckeyList.size(); j++) {
				String uckeyStr = uckeyList.get(j) == null ? "" : uckeyList.get(j).toString();
				if (peopleStr.length() == uckeyStr.length() && peopleStr.equals(uckeyStr)) {
					peopleHashMap.put(peopleStr, j);
					break;
				}
			}
		}
		System.out.println(cityHashMap.size());
		Set citySet = cityHashMap.keySet();
		List<KeyWordModel> cityKeyWorldList = new ArrayList<KeyWordModel>();
		KeyWordModel keyWordModel = null;
		for (Iterator iterator = citySet.iterator(); iterator.hasNext();) {
			String keyword = iterator.next().toString();
			int index = cityHashMap.get(keyword);
			int count = new Integer(countList.get(index).toString());
			System.out.println(keyword + " >>> " + count);
			keyWordModel = new KeyWordModel();
			keyWordModel.setHdKeyWord(keyword);
			keyWordModel.setUcKeyWord(keyword);
			keyWordModel.setCount(count);
			cityKeyWorldList.add(keyWordModel);
		}
		
		System.out.println("===================================");
		List<KeyWordModel> peopleKeyWorldList = new ArrayList<KeyWordModel>();
		Set peopleSet = peopleHashMap.keySet();
		for (Iterator iterator = peopleSet.iterator(); iterator.hasNext();) {
			String keyword = iterator.next().toString();
			int index = peopleHashMap.get(keyword);
			int count = new Integer(countList.get(index).toString());
			System.out.println(keyword + " >>> " + count);
			keyWordModel = new KeyWordModel();
			keyWordModel.setHdKeyWord(keyword);
			keyWordModel.setUcKeyWord(keyword);
			keyWordModel.setCount(count);
			peopleKeyWorldList.add(keyWordModel);
		}
		//排序数组
		Collections.sort(cityKeyWorldList);
		Collections.sort(peopleKeyWorldList);
		//输出excele
		ExportExcel<KeyWordModel> excel = new ExportExcel<KeyWordModel>();
		String[] strs = {"互动百科关键字","UC关键字","搜索次数"};
		try {
			excel.exportExcel("互动百科城市匹配",strs, cityKeyWorldList, new FileOutputStream("F://互动百科城市匹配.xls"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			excel.exportExcel("互动百科人物匹配",strs, peopleKeyWorldList, new FileOutputStream("F://互动百科人物匹配.xls"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date dateEnd = new Date();
		System.out.println((dateEnd.getTime() - dateStart.getTime())/(60*1000));

	}
	/**
	 * 用集合封装文件内容，以行为单�?
	 * @param fileName
	 * @return
	 * List
	 * @author rose
	 */
	public List getListFromFile(String fileName,String encoding){
		List list = new ArrayList();
		InputStream is = null;
		LineNumberReader lnr = null;
		try {
			is = new FileInputStream(fileName);
			lnr = new LineNumberReader(new InputStreamReader(is,encoding));
			String temp = "";
			while((temp = lnr.readLine()) != null){
				list.add(temp);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
